const defaultOptions = {
  debug: false, // Whether this instance should log debug messages.
  automaticOpen: true, // Whether or not the websocket should attempt to connect immediately upon instantiation.
  reconnectInterval: 1000, // The number of milliseconds to delay before attempting to reconnect.
  maxReconnectInterval: 30000, // The maximum number of milliseconds to delay a reconnection attempt.
  reconnectDecay: 1.5, // The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist.
  timeoutInterval: 2000, // The maximum time in milliseconds to wait for a connection to succeed before closing and retrying.
  maxReconnectAttempts: null, // The maximum number of reconnection attempts to make. Unlimited if null.
  binaryType: 'blob' // The binary type, possible values 'blob' or 'arraybuffer', default 'blob'.
}

export default class Socket {
  constructor (options) {
    this.socket = null
    this.status = WebSocket.CLOSED
    for (let key in defaultOptions) {
      if (typeof options[key] !== 'undefined') {
        this[key] = options[key]
      } else {
        this[key] = defaultOptions[key]
      }
    }
  }

  connect (url) {
    this.socket = new WebSocket(url)
    this.socket.onopen = e => {
      console.log('open')
      console.log(e)
    }
    this.socket.onclose = e => {
      console.log('close')
      console.log(e)
    }
    this.socket.onmessage = e => {
      console.log('message')
      console.log(e)
    }
    this.socket.onerror = e => {
      console.log('error')
      console.log(e)
    }
  }

  get isOpen () {
    return this.status === WebSocket.OPEN
  }
}
